package com.kun.valiant.web.controller.valiant;

import com.kun.valiant.activity.service.IActivityService;
import com.kun.valiant.business.domain.Business;
import com.kun.valiant.business.domain.vo.BusinessFollow;
import com.kun.valiant.business.domain.vo.BusinessSummary;
import com.kun.valiant.business.domain.vo.BusinessSummaryEnhance;
import com.kun.valiant.business.domain.vo.BusinessVO;
import com.kun.valiant.business.service.IBusinessService;
import com.kun.valiant.channel.service.IChannelService;
import com.kun.valiant.common.annotation.Log;
import com.kun.valiant.common.core.controller.BaseController;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.core.page.TableDataInfo;
import com.kun.valiant.common.enums.BusinessType;
import com.kun.valiant.course.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 线索 前端控制器
 * @author fangkun
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/valiant/business")
public class BusinessController extends BaseController {

    @Autowired
    IBusinessService businessService;

    @Autowired
    IChannelService channelService;

    @Autowired
    IActivityService activityService;

    @Autowired
    ICourseService courseService;


    /**
     * 商机列表
     * @return
     */
    @GetMapping("/list")
    @PostAuthorize("hasPermission('valiant:business:list')")
    public TableDataInfo list(BusinessVO businessVO){
        startPage();
        List<BusinessSummary> businesses = businessService.getBusinessList(businessVO);
        return getDataTable(businesses);
    }

    /**
     * 添加商机
     * @param business
     * @return
     */
    @PreAuthorize("hasPermission('valiant:business:create')")
    @Log(title = "商机管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addBusiness(@Validated @RequestBody Business business) {
        return businessService.addBusiness(business);
    }

    /**
     * 添加时下拉选择渠道
     * @return
     */
    @PreAuthorize("hasAnyPermissions('valiant:business:create','valiant:business:edit')")
    @GetMapping("/channels")
    public AjaxResult getAllChannels(){
        return  AjaxResult.success(channelService.list());
    }

    /**
     * 添加时下拉选择活动
     * @return
     */
    @PreAuthorize("hasPermission('valiant:business:create')")
    @GetMapping("/activity/{channelId}")
    public AjaxResult getActivityByChannelId(@PathVariable Long channelId){
        return  AjaxResult.success(activityService.getActivityByChannelId(channelId));
    }

    /**
     * 根据类型查询课程
     * @param type
     * @return
     */
    @PostAuthorize("hasAnyPermissions('valiant:business:follow','valiant:business:view')")
    @GetMapping("/course/{type}")
    public AjaxResult getCourseByType(@PathVariable Integer type){
        return courseService.getCourseByType(type);
    }

    /**
     * 根据id查询商机
     * @param id
     * @return
     */
    @PostAuthorize("hasAnyPermissions('valiant:business:follow','valiant:business:view')")
    @GetMapping("/{id}")
    public AjaxResult getBusinessById(@PathVariable Long id){
        return businessService.getBusinessById(id);
    }

    /**
     * 查询所以课程
     * @return
     */
    @PostAuthorize("hasAnyPermissions('valiant:business:follow','valiant:business:view')")
    @GetMapping("/all_course")
    public AjaxResult getAllCourse(){
        return AjaxResult.success(courseService.list());
    }

    /**
     * 跟进商机
     * @param businessFollow
     * @return
     */
    @PreAuthorize("hasPermission('valiant:business:follow')")
    @PostMapping("/follow")
    public AjaxResult follow(@RequestBody BusinessFollow businessFollow){
        return businessService.follow(businessFollow);
    }

    /**
     * 修改模态窗口展示
     * @param businessId
     * @return
     */
    @PreAuthorize("hasPermission('valiant:business:edit')")
    @GetMapping("/summary/{businessId}")
    public AjaxResult getBusinessSummaryById(@PathVariable Long businessId){
        return businessService.getBusinessSummaryById(businessId);
    }

    /**
     * 商机修改
     * @param businessSummaryEnhance
     * @return
     */
    @PreAuthorize("hasPermission('valiant:business:edit')")
    @PutMapping
    public AjaxResult updateBusiness(@RequestBody BusinessSummaryEnhance businessSummaryEnhance){
        return businessService.updateBusiness(businessSummaryEnhance);
    }

    @PreAuthorize("hasPermission('valiant:business:remove')")
    @DeleteMapping("/{businessIds}")
    public AjaxResult deleteBusinessByIds(@PathVariable Long[] businessIds){
        return businessService.deleteBusinessByIds(businessIds);
    }

}
